package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.ModuleSpStockDao;
import com.sugon.entity.ModuleSpStockEntity;
import com.sugon.entity.StoreHouseEntity;
import com.sugon.modules.mold.consts.MoldSpStockState;
import com.sugon.service.*;
import com.sugon.utils.Constant;
import com.sugon.utils.ShiroUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模具备件库存状况Service实现类
 *
 * @author sugon
 * @date 2021-10-13 16:59:06
 */
@Service("moduleSpStockService")
public
class ModuleSpStockServiceImpl implements ModuleSpStockService {
    @Resource
    private ModuleSpStockDao moduleSpStockDao;
    @Resource
    private ModuleSpPurchasingBatchService moduleSpPurchasingBatchService;
    @Resource
    private StoreHouseService storeHouseService;
    @Resource
    private ModuleSpDeptStockService moldSpDeptStockService;
    @Resource
    private ModuleSpDemandItemService moldSpDemandItemService;

    /**
     * @param uniqueCode
     * @return
     */
    @Override
    public ModuleSpStockEntity queryListByUniqueCode(String uniqueCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("uniqueCode", uniqueCode);
        List<ModuleSpStockEntity> moduleSpStockEntities = queryList(param);
        return CollectionUtil.isNotEmpty(moduleSpStockEntities) ? moduleSpStockEntities.get(0) : null;
    }

    @Override
    public List<ModuleSpStockEntity> queryListByDemandItemId(String demandItemId) {
        Map<String, Object> param = new HashMap<>();
        param.put("demandItemId", demandItemId);
        return queryList(param);
    }

    /**
     * @param id 主键
     * @return
     */
    @Override
    public ModuleSpStockEntity queryObject(String id) {
        return moduleSpStockDao.queryObject(id);
    }

    /**
     * 查询
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<ModuleSpStockEntity> queryList(Map<String, Object> map) {
        return moduleSpStockDao.queryList(map);
    }

    /**
     * 查询总数
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleSpStockDao.queryTotal(map);
    }

    /**
     * 确认入库
     *
     * @param workshop 车间ID
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean confirmStockIn(String workshop) {
        // 获取待入库的型芯列表
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("state", MoldSpStockState.ING);
        queryParam.put("workshop", workshop);
        List<ModuleSpStockEntity> moduleSpStockList = queryList(queryParam);
        if (CollectionUtil.isEmpty(moduleSpStockList)) {
            return true;
        }
        // 获取模具型芯备件车间仓库
        StoreHouseEntity storeHouse = storeHouseService.queryByWorkshop(3, workshop);
        Assert.notNull(storeHouse, "当前车间尚未创建车间仓库，请确认！");
        // 执行入库
        for (ModuleSpStockEntity spStock : moduleSpStockList) {
            spStock.setState(MoldSpStockState.ED);
            spStock.setStockInDate(DateUtil.date());
            spStock.setStoreId(storeHouse.getId());
            int updateNum = update(spStock);
            if (updateNum > 0) {
                // 更新对应型芯计划明细中的入库和在途数
                moldSpDemandItemService.realtimeUpdateNum(spStock.getDemandItemId());
                // 实时更新车间型芯库存信息
                moldSpDeptStockService.realtimeUpdateDeptStock(spStock.getWorkshop(), spStock.getMoldSpId());
            }
        }
        // 实时更新批次状态
        return moduleSpPurchasingBatchService.realtimePurchasingBacthStockState();
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public boolean confirmStockOut(String[] ids) {
        // 获取待入库的型芯列表
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("state", MoldSpStockState.LOCKING);
        queryParam.put("ids", ids);
        List<ModuleSpStockEntity> moduleSpStockList = queryList(queryParam);
        if (CollectionUtil.isEmpty(moduleSpStockList)) {
            return true;
        }
        // 执行入库
        for (ModuleSpStockEntity spStock : moduleSpStockList) {
            spStock.setState(MoldSpStockState.USED);
            spStock.setStockOutDate(DateUtil.date());
            spStock.setRemark("扫码出库");
            int updateNum = update(spStock);
            if (updateNum > 0) {
                // 实时更新车间型芯库存信息
                moldSpDeptStockService.realtimeUpdateDeptStock(spStock.getWorkshop(), spStock.getMoldSpId());
            }
        }
        // 实时更新批次状态
        return moduleSpPurchasingBatchService.realtimePurchasingBacthStockState();
    }

    /**
     * 保存模具型芯库存
     *
     * @param moduleSpStock 实体
     * @return
     */
    @Override
    public int save(ModuleSpStockEntity moduleSpStock) {
        moduleSpStock.setId(RandomUtil.randomString(32));
        moduleSpStock.setDelFlag(Constant.IS_NOT_DEL_INT);
        moduleSpStock.setGmtCreate(DateUtil.date());
        moduleSpStock.setCreateUser(ShiroUtils.getUserId());
        // 设置所属车间和仓库ID
        String workshop = ShiroUtils.getWorkshop();
        if (StrUtil.isNotEmpty(workshop)) {
            // 设置所属车间
            moduleSpStock.setWorkshop(workshop);
            StoreHouseEntity storeHouse = storeHouseService.queryByWorkshop(3, workshop);
            Assert.notNull(storeHouse, "请在创建车间仓库后再进行该操作！");
            moduleSpStock.setStoreId(storeHouse.getId());
        }
        return moduleSpStockDao.save(moduleSpStock);
    }

    /**
     * 更新
     *
     * @param moduleSpStock 实体
     * @return
     */
    @Override
    public int update(ModuleSpStockEntity moduleSpStock) {
        moduleSpStock.setGmtModified(DateUtil.date());
        moduleSpStock.setModifyUser(ShiroUtils.getUserId());
        return moduleSpStockDao.update(moduleSpStock);
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return moduleSpStockDao.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return moduleSpStockDao.deleteBatch(ids);
    }
}
